/* 
 *  Copyright (c) 2011—2013 Panguso.com. All rights reserved. 
 *  File Name:  LogTokenizerHelper.java                                      
 *  File Contents Outline :  日志分隔符持久化类
 *  File  Create Date:   2012年7月30日
 *  File Version#: Revision#: #1                  
 *  File  Latest Modify Date : 2013年1月16日
 *  File Author :  bachuan@panguso.com
 */
package storage.dbhelper;

import cn.edu.thu.log.web.service.PropertiesReaderServices;
import cn.edu.thu.log.web.service.impl.PropertiesReaderServicesImpl;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.cassandra.thrift.ColumnOrSuperColumn;
import org.apache.cassandra.thrift.KeySlice;

import storage.dbobject.DbLogTokenizer;

import storage.util.StorageManager;

public class LogTokenizerHelper {

    /**
     * 将logTokenizer保存到LaUDMS数据库中
     *
     * @param logTokenizer 要存储的模型对象
     */
    public void saveLogTokenizer(DbLogTokenizer logTokenizer) {
      //  StorageManager sm = new StorageManager(StorageParameter.db_keySpace, StorageParameter.db_columnFamily_logTokenizer);
                                        PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
        StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"), x1.getProperties("db_columnFamily_logTokenizer"));
//        sm.open();
        sm.setRowKey(logTokenizer.getRowKey());
        sm.put(DbLogTokenizer.cl_name, logTokenizer.getName());
        sm.put(DbLogTokenizer.cl_value, logTokenizer.getValue());
//        sm.put(logTokenizer.cl_chinese, logTokenizer.getChinese());
//        sm.close();

    }

    /**
     * 将一系列logTokenizer保存到LaUDMS数据库中
     *
     * @param logTokenizers 要存储的模型对象列表
     */
    public void saveLogTokenizers(List<DbLogTokenizer> logTokenizers) {
//        StorageManager sm = new StorageManager(StorageParameter.db_keySpace, StorageParameter.db_columnFamily_logTokenizer);
                                              PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
        StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"), x1.getProperties("db_columnFamily_logTokenizer"));
//        sm.open();
        for (int i = 0; i < logTokenizers.size(); i++) {
            DbLogTokenizer logTokenizer = logTokenizers.get(i);
            sm.setRowKey(logTokenizer.getRowKey());
            sm.put(DbLogTokenizer.cl_name, logTokenizer.getName());
            sm.put(DbLogTokenizer.cl_value, logTokenizer.getValue());
//            sm.put(logTokenizer.cl_chinese, logTokenizer.getChinese());
        }
//        sm.close();
    }

    public List<DbLogTokenizer> getLogTokenizers(final String columnName, final String value) {
        List<DbLogTokenizer> result = new LinkedList<DbLogTokenizer>();
      //  StorageManager sm = new StorageManager(StorageParameter.db_keySpace, StorageParameter.db_columnFamily_logTokenizer);
                                              PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
        StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"), x1.getProperties("db_columnFamily_logTokenizer"));
//        sm.open();
        List<KeySlice> crs = sm.get(columnName, value);
        try {
            for (KeySlice ks : crs) {
                String rowKey = new String(ks.getKey());
                String name = null, valueL = null, chinese = null;
                for (ColumnOrSuperColumn column : ks.getColumns()) {
                    String columnsName = new String(column.getColumn().getName());
                    String columnsValue = new String(column.getColumn().getValue());
                    if (columnsName.equals(DbLogTokenizer.cl_name)) {
                        name = columnsValue;
                    } else if (columnsName.equals(DbLogTokenizer.cl_value)) {
                        valueL = columnsValue;
                    }
                }
                DbLogTokenizer pLogTokenizer = new DbLogTokenizer(rowKey, name, valueL);
                result.add(pLogTokenizer);
            }
        } catch (Exception ex) {
            Logger.getLogger(LogTokenizerHelper.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
//            sm.close();
        }
        return result;
    }

    public void deleteLogTokenizer(final String rowKey) {
       // StorageManager sm = new StorageManager(StorageParameter.db_keySpace, StorageParameter.db_columnFamily_logTokenizer);
                                              PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
        StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"), x1.getProperties("db_columnFamily_logTokenizer"));
//        sm.open();
        sm.delete(rowKey);
//        sm.close();
    }

    public ArrayList<String> getAllLogTokenizerName() {
   //     StorageManager sm = new StorageManager(StorageParameter.db_keySpace, StorageParameter.db_columnFamily_logTokenizer);
                                              PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
        StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"), x1.getProperties("db_columnFamily_logTokenizer"));
//        sm.open();
        List<KeySlice> crs = sm.getAll();
        ArrayList<String> logTokenizerNameList = new ArrayList<String>();
        try {
            for (KeySlice ks : crs) {
                String name = null;
                for (ColumnOrSuperColumn column : ks.getColumns()) {
                    String columnsName = new String(column.getColumn().getName());
                    String columnsValue = new String(column.getColumn().getValue());
                    if (columnsName.equals(DbLogTokenizer.cl_name)) {
                        name = columnsValue;
                    }
                }
                if (!logTokenizerNameList.contains(name)) {
                    logTokenizerNameList.add(name);
                }
            }
        } catch (Exception ex) {
            Logger.getLogger(LogTokenizerHelper.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
//            sm.close();
        }
        return logTokenizerNameList;
    }
}
